package cn.jx.asc.service.impl;

import cn.jx.asc.common.utils.StringUtils;
import cn.jx.asc.dao.mapper.ThresholdMapper;
import cn.jx.asc.pojo.entity.Threshold;
import cn.jx.asc.service.ThresholdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lhg
 * @since 2021-06-28
 */
@Service
public class ThresholdServiceImpl extends ServiceImpl<ThresholdMapper, Threshold> implements ThresholdService {
    @Autowired
    private ThresholdMapper thresholdMapper;

    @Override
    public boolean updateThreshold(Threshold threshold) {
        final String maxLiquidLevel = threshold.getMaxLiquidLevel();
        final String minLiquidLevel = threshold.getMinLiquidLevel();
        final String maxRainfull = threshold.getMaxRainfull();
        final String maxSoilTemperature = threshold.getMaxSoilTemperature();
        final String maxAngle = threshold.getMaxAngle();
        final String maxInjectionVolume = threshold.getMaxInjectionVolume();
        final String maxAirTemperature = threshold.getMaxAirTemperature();
        final String maxAirHumidity = threshold.getMaxAirHumidity();
        final String minAirTemperature = threshold.getMinAirTemperature();
        final String minSoilTemperature = threshold.getMinSoilTemperature();
        final String maxSoilHumidity = threshold.getMaxSoilHumidity();
        if (StringUtils.isEmpty(maxLiquidLevel)||StringUtils.isEmpty(minLiquidLevel)||maxRainfull==null||
                StringUtils.isEmpty(maxSoilTemperature)||StringUtils.isEmpty(maxAngle)||maxInjectionVolume==null||
                StringUtils.isEmpty(maxAirTemperature)||StringUtils.isEmpty(maxAirHumidity)|| StringUtils.isEmpty(minAirTemperature)||
                StringUtils.isEmpty(minSoilTemperature)|| StringUtils.isEmpty(maxSoilHumidity)){
            return false;
        }
        final int i = thresholdMapper.updateById(threshold);
        return i == 1;
    }

    @Override
    public Threshold queryThreshold(Integer id) {
        final Threshold threshold = thresholdMapper.selectById(id);
        return dataFormatting(threshold);
    }
    Threshold dataFormatting(Threshold threshold){
        final DecimalFormat df = new DecimalFormat("0.0");
        final Threshold copyThreshold = new Threshold();
        copyThreshold.setId(threshold.getId());
        copyThreshold.setMaxAirHumidity(df.format(new Double(threshold.getMaxAirHumidity())*0.1));
        copyThreshold.setMaxAngle(df.format(new Double(threshold.getMaxAngle())*0.1));
        copyThreshold.setMaxInjectionVolume(df.format(new Double(threshold.getMaxInjectionVolume())*0.1));
        copyThreshold.setMaxSoilHumidity(df.format(new Double(threshold.getMaxSoilHumidity())*0.1));
        copyThreshold.setMaxSoilTemperature(df.format(new Double(threshold.getMaxSoilTemperature())*0.1));
        copyThreshold.setMinSoilTemperature(df.format(new Double(threshold.getMinSoilTemperature())*0.1));
        copyThreshold.setMaxAirTemperature(df.format(new Double(threshold.getMaxAirTemperature())*0.1));
        copyThreshold.setMinAirTemperature(df.format(new Double(threshold.getMinAirTemperature())*0.1));
        copyThreshold.setMaxAirHumidity(df.format(new Double(threshold.getMaxAirHumidity())*0.1));
        copyThreshold.setMaxRainfull(df.format(new Double(threshold.getMaxRainfull()) * 0.1));
        copyThreshold.setMaxLiquidLevel(df.format(new Double(threshold.getMaxLiquidLevel())*0.1));
        copyThreshold.setMinLiquidLevel(df.format(new Double(threshold.getMinLiquidLevel())*0.1));
        return copyThreshold;

    }
}
